package com.zcs.app.lucky.ui.clip;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.zcs.autolayout.utils.AutoUtils;

/**
 * Created by ZengCS on 2019/5/31.
 * E-mail:zcs@sxw.cn
 * Add:成都市天府软件园E3-3F
 */
public class ClipOverlayView extends View {
    public static final int BORDER_DISTANCE = 100;// 距离屏幕的边距

    private final Paint mPaint = new Paint();
    private final Paint mSpotlightPaint = new Paint();
    private final Path mPath = new Path();
    private final RectF rectF = new RectF();
    private int mWidth, mHeight;

    private boolean isCircle = true;

    public void setIsCircle(boolean circle) {
        isCircle = circle;
    }

    // 勾画相关
    private Bitmap mSpotlightBitmap = null;
    private Canvas mSpotlightCanvas = null;

    public ClipOverlayView(Context context) {
        this(context, null);
    }

    public ClipOverlayView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ClipOverlayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    private void init() {
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(AutoUtils.getPercentWidthSize(5));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);

        mSpotlightPaint.setAlpha(0);
        //这个属性是设置paint为橡皮擦重中之重
        //这是重点
        //下面这句代码是橡皮擦设置的重点
        mSpotlightPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        //上面这句代码是橡皮擦设置的重点（重要的事是不是一定要说三遍）
        mSpotlightPaint.setAntiAlias(true);
        mSpotlightPaint.setDither(true);
        mSpotlightPaint.setStyle(Paint.Style.FILL);
        mSpotlightPaint.setStrokeCap(Paint.Cap.ROUND);
        mSpotlightPaint.setStrokeJoin(Paint.Join.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        ensureSpotlightBitmap();
        if (mSpotlightBitmap != null) {// 聚光灯
            canvas.drawBitmap(mSpotlightBitmap, 0, 0, mPaint);
        }
    }

    private void ensureSpotlightBitmap() {
        if (mSpotlightBitmap == null) {
            mSpotlightBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            mSpotlightCanvas = new Canvas(mSpotlightBitmap);
        }
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    /**
     * 清空画布
     */
    public void clean() {
        mPath.reset();
        mSpotlightCanvas = null;
        mSpotlightBitmap = null;
        invalidate();
    }

    /**
     * 移动聚光灯
     */
    public void openSpotlight() {
        ensureSpotlightBitmap();
        // 清空内容
        mSpotlightCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        mSpotlightCanvas.drawColor(Color.argb(128, 0, 0, 0));

        boolean isHorizontal = false;
        if (mWidth > mHeight) {
            isHorizontal = true;
        }
        int borderLength = isHorizontal ? (mHeight - BORDER_DISTANCE * 2) : (mWidth - BORDER_DISTANCE * 2);
        int inLeft = isHorizontal ? ((mWidth - borderLength) / 2) : BORDER_DISTANCE;
        int inTop = isHorizontal ? BORDER_DISTANCE : ((mHeight - borderLength) / 2);
        int inRight = isHorizontal ? (inLeft + borderLength) : borderLength + BORDER_DISTANCE;
        int inBottom = isHorizontal ? borderLength + BORDER_DISTANCE : (inTop + borderLength);

        rectF.left = inLeft;
        rectF.top = inTop;
        rectF.right = inRight;
        rectF.bottom = inBottom;

        if (isCircle)
            mSpotlightCanvas.drawOval(rectF, mSpotlightPaint);
        else
            mSpotlightCanvas.drawRect(rectF, mSpotlightPaint);
        invalidate();
    }
}
